package com.jiyun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

/**
 * 授权信息的存储和校验
 */

@Configuration
//三项 生成Token
//TODO
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Lazy
    @Resource
    private AuthenticationManager authenticationManager;

    /*    @Autowired
        private DataSource dataSource;*/
    @Resource
    private ClientDetailsService uacClientDetailsService;

    @Resource
    private UserDetailsService uacUserDetailService;

    @Resource
    private RedisConnectionFactory redisConnectionFactory;


    @Bean
      public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        AuthenticationManager authenticationManager =  authenticationConfiguration.getAuthenticationManager();
        return authenticationManager;
    }
    @Bean
    public TokenStore tokenStore(){
        //使用redis存储token信息
        return new RedisTokenStore(redisConnectionFactory);
    }


    @Bean
    public DefaultTokenServices buildTokenService() {
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setClientDetailsService(uacClientDetailsService);
        tokenServices.setTokenStore(tokenStore());
        return tokenServices;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //clients.jdbc(dataSource);
        //clients.
        clients.withClientDetails(uacClientDetailsService);

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //endpoints.authenticationManager(authenticationManager);
        endpoints
                .tokenServices(buildTokenService())
                .authenticationManager(authenticationManager)
                .userDetailsService(uacUserDetailService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
        //TODO 干什么用的？什么意思？
        security.checkTokenAccess("permitAll()");
        //TODO 验证
        security.tokenKeyAccess("permitAll()");

    }
}
